package com.yixing.tech.system.controller;

import com.yixing.tech.common.base.BaseControl;
import com.yixing.tech.common.base.BusinessException;
import com.yixing.tech.common.base.Rs;
import com.yixing.tech.common.service.CommonService;
import com.yixing.tech.system.service.IMenuService;
import com.yixing.tech.common.vo.s3.MenuVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.yixing.tech.common.security.context.UserContext.getTenantId;

/**
 * @author yixing tech
 * at 2021/12/20
 * describe：
 */
@RestController
@CrossOrigin
@RequestMapping(value = "/menu")
public class MenuController extends BaseControl {

    @Resource
    IMenuService menuService;

    @Autowired
    private CommonService commonService;

    @PostMapping("/add")
    public Rs<Object> add(@RequestBody MenuVO vo) throws BusinessException {
        commonService.fillCreateUser(vo);
        menuService.add(vo);
        return Rs.ok();
    }

    @PostMapping("/add/child")
    public Rs<Object> addChild(@RequestBody MenuVO vo) throws BusinessException {
        commonService.fillCreateUser(vo);
        menuService.addChild(vo);
        return Rs.ok();
    }

    @PostMapping("/update/{id}")
    public Rs<Object> update(@RequestBody MenuVO vo, @PathVariable String id) throws BusinessException {
        commonService.fillUpdateUser(vo);
        vo.setId(id);
        vo.setTenantId(getTenantId());
        menuService.update(vo);
        return Rs.ok();
    }

    @PostMapping("/list")
    public Rs<Object> list(@RequestBody MenuVO vo) throws BusinessException {
        vo.setTenantId(getTenantId());
        List<MenuVO> list = menuService.list(vo, commonService.getApiUser());
        return Rs.ok(list);
    }

    @PostMapping("/list/self")
    public Rs<Object> listSelf(@RequestBody MenuVO vo) throws BusinessException {
        vo.setTenantId(getTenantId());
        List<MenuVO> list = menuService.list(vo, commonService.getApiUser());
        return Rs.ok(list);
    }

    @DeleteMapping()
    public Rs<Object> delete(@RequestBody MenuVO vo) throws BusinessException {
        vo.setTenantId(getTenantId());
        menuService.delete(vo);
        return Rs.ok();
    }

    @GetMapping("/{id}")
    public Rs<Object> get(@PathVariable String id) throws BusinessException {
        return Rs.ok(menuService.queryById(id));
    }

}
